﻿/******************************************************/
// RiceInfoPage プレゼンテーションクラス  
/******************************************************/

using System;
using System.Data;
using System.Globalization;
using System.Resources;
using System.Threading;
using System.Web.UI;
using System.Web.UI.WebControls;
using MAA.Business;
using MAA.Entity;

namespace MAA.Presentation.PRL.News
{
    public partial class RiceInfoPage : System.Web.UI.Page
    {
        private ResourceManager rm = new ResourceManager("MAA.Presentation.language", System.Reflection.Assembly.GetExecutingAssembly());
        private CultureInfo m_JapanCulture = new CultureInfo("ja-JP");
        private CultureInfo m_VietnameCulture = new CultureInfo("vi");

        private void UpdateUI()
        {
            imgBtnFirst.ToolTip = rm.GetString("langFirst");
            imgBtnPrevious.ToolTip = rm.GetString("langPrevious");
            imgBtnNext.ToolTip = rm.GetString("langNext");
            imgBtnLast.ToolTip = rm.GetString("langLast");
            //Label langOrigin;
            //Label langDes;
            //string origin = rm.GetString("langOrigin");
            //string des = rm.GetString("langDescription");
            //for (int i = 0; i < tblRiceInfo.Items.Count; i++)
            //{
            //    langOrigin = (Label)tblRiceInfo.Items[i].FindControl("langOrigin");
            //    langDes = (Label)tblRiceInfo.Items[i].FindControl("langDes");
            //    langOrigin.Text = origin;
            //    langDes.Text = des;
            //}
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["language"] == null)
            {
                Session["language"] = "vi";
            }
            if (Session["language"].Equals("vi"))
            {
                Thread.CurrentThread.CurrentUICulture = m_VietnameCulture;
                UpdateUI();
            }
            if (Session["language"].Equals("jp"))
            {
                Thread.CurrentThread.CurrentUICulture = m_JapanCulture;
                UpdateUI();
            }
            if (IsPostBack) return;
            Session["pageIndex"] = 1;
            LoadRice();
        }

        private void LoadRice()
        {
            BOLNews bolNews = new BOLNews();
            DataTable tblRice = new DataTable();
            string message = "";
            long rowNum = 0;
            int pageIndex = Convert.ToInt32(Session["pageIndex"]);
            int pageSize = Common.PAGESIZE_RICE;

            string lang = Session["language"].ToString();
            // View information of all rice type
            if (bolNews.viewRiceInfo(lang,pageIndex, pageSize, out rowNum, out tblRice, out message) == true)
            {
                DataView riceDataView = new DataView(tblRice);
                PagedDataSource riceItems = new PagedDataSource();
                riceItems.DataSource = riceDataView;
                tblRiceInfo.DataSource = riceItems;
                tblRiceInfo.DataBind();
                if (rowNum > pageSize)
                {
                    Pager(rowNum, pageIndex);
                    enablePaging(true);
                }
                else
                {
                    enablePaging(false);
                }
            }
            else
            {
                enablePaging(false);
                tblRiceInfo.DataSource = null;
                tblRiceInfo.DataBind();
                ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(),
                    "Alert", "<script>alert('" + PRLCommon.ChangeLanguageMessage(message) + "')</script>", false);
            }
        }

        private void Pager(long recordCount, int currentPage)
        {
            lblCurrentPage.Text = currentPage.ToString();
            double dblPageCount = (double)((decimal)recordCount / decimal.Parse(Common.PAGESIZE_RICE.ToString()));
            int pageCount = (int)Math.Ceiling(dblPageCount);
            lblTotalPage.Text = pageCount.ToString();
        }

        /// <summary>
        /// Processing when user changes page
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Changed(object sender, EventArgs e)
        {
            int pageIndex = int.Parse(Session["pageIndex"].ToString());
            int pageCount = Convert.ToInt32(lblTotalPage.Text);

            if ((sender as ImageButton).CommandArgument == "First")
            {
                if (pageIndex == 1) return;
                pageIndex = 1;
            }

            if ((sender as ImageButton).CommandArgument == "Previous")
            {
                pageIndex = pageIndex - 1;
                if (pageIndex < 1) return;
            }

            if ((sender as ImageButton).CommandArgument == "Next")
            {
                pageIndex = pageIndex + 1;
                if (pageIndex > pageCount) return;
            }

            if ((sender as ImageButton).CommandArgument == "Last")
            {
                if (pageIndex == pageCount) return;
                pageIndex = pageCount;
            }
            Session["pageIndex"] = pageIndex;
            LoadRice();
        }


        private void enablePaging(bool enable)
        {
            if (enable == true)
            {
                imgBtnFirst.Visible = true;
                imgBtnPrevious.Visible = true;
                lblCurrentPage.Visible = true;
                lblSeperate.Visible = true;
                lblTotalPage.Visible = true;
                imgBtnNext.Visible = true;
                imgBtnLast.Visible = true;
            }
            else
            {
                imgBtnFirst.Visible = false;
                imgBtnPrevious.Visible = false;
                lblCurrentPage.Visible = false;
                lblSeperate.Visible = false;
                lblTotalPage.Visible = false;
                imgBtnNext.Visible = false;
                imgBtnLast.Visible = false;
            }
        }

        protected void tblRiceInfo_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Label langOrigin = (Label)e.Item.FindControl("langOrigin");
                Label langDes = (Label)e.Item.FindControl("langDes");
                langOrigin.Text = rm.GetString("langOrigin");
                langDes.Text = rm.GetString("langDescription");
            }
        }
    }
}